const express = require('express');
const router = express.Router();
const {
  productListCache,
  productDetailCache,
  invalidateProductCache
} = require('../middlewares/cache.middleware');
const {
  adminLogin,
  getCurrentAdmin,
  adminLogout,
  refreshToken,
  changePassword,
  getDashboardStats,
  getSalesTrend,
  getRegionStats,
  getOrderStats
} = require('../controllers/admin.controller');
const {
  getProducts,
  getProduct,
  createProduct,
  updateProduct,
  deleteProduct,
  batchDeleteProducts,
  batchUpdateProducts
} = require('../controllers/admin.product.controller');
const {
  getShops,
  getShop,
  createShop,
  updateShop,
  deleteShop,
  approveShop,
  suspendShop,
  resumeShop,
  getPendingShops,
  getShopStats,
  batchActionShops,
  getShopProducts,
  getApprovalHistory,
  getShopApprovalHistory,
  getApprovalStats,
  getOverallStats,
  getShopRankings,
  getRevenueTrend,
  getCategoryStats
} = require('../controllers/admin.shop.controller');
const {
  getUsers,
  getUser,
  createUser,
  updateUser,
  deleteUser,
  batchDeleteUsers,
  updateUserStatus,
  getUserStats,
  searchUsers
} = require('../controllers/admin.user.controller');
const { adminAuth, checkRole, checkPermission } = require('../middlewares/admin.middleware');
const { validateProductImages } = require('../middlewares/image.validation.middleware');

// 认证相关路由
router.post('/auth/login', adminLogin);
router.get('/auth/me', adminAuth, getCurrentAdmin);
router.post('/auth/logout', adminAuth, adminLogout);
router.post('/auth/refresh', adminAuth, refreshToken);
router.post('/auth/change-password', adminAuth, changePassword);

// 仪表板路由
router.get('/dashboard/stats', adminAuth, getDashboardStats);
router.get('/dashboard/sales-trend', adminAuth, getSalesTrend);
router.get('/dashboard/region-stats', adminAuth, getRegionStats);
router.get('/dashboard/order-stats', adminAuth, getOrderStats);

// 用户管理路由
router.get('/users/stats', adminAuth, checkPermission(['user:view']), getUserStats);
router.get('/users/search', adminAuth, checkPermission(['user:view']), searchUsers);
router.post('/users/batch-delete', adminAuth, checkPermission(['user:delete']), batchDeleteUsers);
router.get('/users', adminAuth, checkPermission(['user:view']), getUsers);
router.get('/users/:id', adminAuth, checkPermission(['user:view']), getUser);
router.post('/users', adminAuth, checkPermission(['user:create']), createUser);
router.put('/users/:id', adminAuth, checkPermission(['user:update']), updateUser);
router.delete('/users/:id', adminAuth, checkPermission(['user:delete']), deleteUser);
router.patch('/users/:id/status', adminAuth, checkPermission(['user:update']), updateUserStatus);

// 店铺管理路由
router.get('/shops/pending', adminAuth, checkPermission(['shop:approve']), getPendingShops);
router.get('/shops/approval-history', adminAuth, checkPermission(['shop:view']), getApprovalHistory);
router.get('/shops/approval-stats', adminAuth, checkPermission(['shop:view']), getApprovalStats);
router.get('/shops/overall-stats', adminAuth, checkPermission(['shop:view']), getOverallStats);
router.get('/shops/rankings', adminAuth, checkPermission(['shop:view']), getShopRankings);
router.get('/shops/revenue-trend', adminAuth, checkPermission(['shop:view']), getRevenueTrend);
router.get('/shops/category-stats', adminAuth, checkPermission(['shop:view']), getCategoryStats);
router.post('/shops/batch-action', adminAuth, checkPermission(['shop:update']), batchActionShops);
router.get('/shops', adminAuth, checkPermission(['shop:view']), getShops);
router.get('/shops/:id', adminAuth, checkPermission(['shop:view']), getShop);
router.post('/shops', adminAuth, checkPermission(['shop:create']), createShop);
router.put('/shops/:id', adminAuth, checkPermission(['shop:update']), updateShop);
router.delete('/shops/:id', adminAuth, checkPermission(['shop:delete']), deleteShop);
router.post('/shops/:id/approve', adminAuth, checkPermission(['shop:approve']), approveShop);
router.post('/shops/:id/suspend', adminAuth, checkPermission(['shop:update']), suspendShop);
router.post('/shops/:id/resume', adminAuth, checkPermission(['shop:update']), resumeShop);
router.get('/shops/:id/stats', adminAuth, checkPermission(['shop:view']), getShopStats);
router.get('/shops/:id/products', adminAuth, checkPermission(['shop:view', 'product:view']), getShopProducts);
router.get('/shops/:id/approval-history', adminAuth, checkPermission(['shop:view']), getShopApprovalHistory);

// 商品管理路由
router.get('/products', adminAuth, checkPermission(['product:view']), productListCache, getProducts);
router.get('/products/:id', adminAuth, checkPermission(['product:view']), productDetailCache, getProduct);
router.post('/products', adminAuth, checkPermission(['product:create']), validateProductImages, invalidateProductCache, createProduct);
router.put('/products/:id', adminAuth, checkPermission(['product:update']), validateProductImages, invalidateProductCache, updateProduct);
router.delete('/products/:id', adminAuth, checkPermission(['product:delete']), invalidateProductCache, deleteProduct);
router.post('/products/batch-delete', adminAuth, checkPermission(['product:delete']), invalidateProductCache, batchDeleteProducts);
router.post('/products/batch-update', adminAuth, checkPermission(['product:update']), validateProductImages, invalidateProductCache, batchUpdateProducts);

// 订单管理路由
const { getOrders, getOrderByIdAdmin, updateOrderStatus } = require('../controllers/order.controller');
router.get('/orders', adminAuth, checkPermission(['order:view']), getOrders);
router.get('/orders/:id', adminAuth, checkPermission(['order:view']), getOrderByIdAdmin);
router.patch('/orders/:id/status', adminAuth, checkPermission(['order:update']), updateOrderStatus);

// 优惠券管理路由
router.get('/coupons', adminAuth, checkPermission(['coupon:view']), (req, res) => {
  res.json({ message: '优惠券列表功能待实现' });
});

router.post('/coupons', adminAuth, checkPermission(['coupon:create']), (req, res) => {
  res.json({ message: '创建优惠券功能待实现' });
});

// 轮播图管理路由
const adminBannerController = require('../controllers/admin.banner.controller');
const {
  createBannerValidation,
  updateBannerValidation,
  getBannerValidation,
  deleteBannerValidation,
  batchDeleteBannersValidation,
  updateBannerStatusValidation,
  getBannersValidation
} = require('../middlewares/banner.validator');

// 获取轮播图列表
router.get('/banners', adminAuth, checkPermission(['banner:view']), getBannersValidation, adminBannerController.getBanners);

// 获取单个轮播图详情
router.get('/banners/:id', adminAuth, checkPermission(['banner:view']), getBannerValidation, adminBannerController.getBanner);

// 创建轮播图
router.post('/banners', adminAuth, checkPermission(['banner:create']), createBannerValidation, adminBannerController.createBanner);

// 更新轮播图
router.put('/banners/:id', adminAuth, checkPermission(['banner:edit']), updateBannerValidation, adminBannerController.updateBanner);

// 删除轮播图
router.delete('/banners/:id', adminAuth, checkPermission(['banner:delete']), deleteBannerValidation, adminBannerController.deleteBanner);

// 批量删除轮播图
router.post('/banners/batch-delete', adminAuth, checkPermission(['banner:delete']), batchDeleteBannersValidation, adminBannerController.batchDeleteBanners);

// 更新轮播图状态
router.patch('/banners/:id/status', adminAuth, checkPermission(['banner:edit']), updateBannerStatusValidation, adminBannerController.updateBannerStatus);

// 文件上传路由
const uploadController = require('../controllers/upload.controller');
router.post('/upload/:type', adminAuth, uploadController.uploadSingle);
router.post('/upload/:type/multiple', adminAuth, uploadController.uploadMultiple);
router.delete('/upload/:type/:filename', adminAuth, uploadController.deleteFile);

// 销售数据路由
router.get('/sales/overview', adminAuth, checkPermission(['sales:view']), (req, res) => {
  res.json({ message: '销售概览功能待实现' });
});

router.get('/sales/analytics', adminAuth, checkPermission(['sales:view']), (req, res) => {
  res.json({ message: '销售分析功能待实现' });
});

// 客服管理路由
router.get('/chat/sessions', adminAuth, checkPermission(['chat:view']), (req, res) => {
  res.json({ message: '客服会话列表功能待实现' });
});

router.post('/chat/messages', adminAuth, checkPermission(['chat:reply']), (req, res) => {
  res.json({ message: '发送客服消息功能待实现' });
});

// 系统设置路由
router.get('/system/settings', adminAuth, checkPermission(['system:config']), (req, res) => {
  res.json({ message: '系统设置功能待实现' });
});

router.get('/system/logs', adminAuth, checkPermission(['system:log']), (req, res) => {
  res.json({ message: '系统日志功能待实现' });
});

module.exports = router;
